//
//  DetaLisViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DetaLisViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "AFNetBlock.h"
#import <UIImageView+WebCache.h>
#import "DataModel.h"
#import "DetaTableViewCell.h"
#import "PlayViewController.h"
#import "BarModel.h"
#import "DataBaseManager.h"

@interface DetaLisViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dateArr;
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)UIView *header;
@property(nonatomic, retain)UIButton *buttOne;
@property(nonatomic, assign)BOOL isPlay;// 是否收藏

@end

@implementation DetaLisViewController
- (void)dealloc
{
    [_tableView release];
    [_dateArr release];
    [_button release];
    [_header release];
    [_button release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _name;

#pragma mark - 数据库查询
    // 查询
    NSArray *array = [[DataBaseManager shareInstance] select];
    // 判断是否收藏
    NSInteger mark = 0;
    for (Listen *model in array) {
        if ([model.listenName isEqualToString:self.navigationItem.title]) {
            //如果收藏就加1
            mark++;
        }
    }
    // 当没有收藏
    if (mark == 0) {
        
        self.isPlay = YES;
    }
    //如果收藏
    else{
        self.isPlay = NO;
    }
  
    /** 创建tableView **/
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 70) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_tableView];
    [_tableView release];
   
    /** 数据获取 **/
    [self detaHandler];
    /** 头部区域 **/
    [self headers];
    
      // 重用池
    [self.tableView registerClass:[DetaTableViewCell class] forCellReuseIdentifier:@"cell"];
  
    // 设置 头部区域控件
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,300)];
    img.backgroundColor = [UIColor redColor];
    // 添加图片
    [img sd_setImageWithURL:[NSURL URLWithString:_image]];
    [_header addSubview:img];
    
    [img release];
    // 按钮创建
//    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
//    _button.frame = CGRectMake(160, 210, 50, 50);
////    self.button.backgroundColor = [UIColor yellowColor];
//    [self.header addSubview:_button];
//    [self.button addTarget:self action:@selector(butt) forControlEvents:UIControlEventTouchUpInside];
//    [_header addSubview:_button];
//    [_button setImage:[UIImage imageNamed:@"bf"] forState:UIControlStateNormal];
    
    //订阅按钮
    self.buttOne = [UIButton buttonWithType:UIButtonTypeCustom];
//    _buttOne.frame = CGRectMake(115, 210, 20, 20);
    [_buttOne addTarget:self action:@selector(buttOne) forControlEvents:UIControlEventTouchUpInside];
    [_header addSubview:_buttOne];
    [_buttOne setBackgroundImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
    
    // 收藏按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"x"] style:UIBarButtonItemStylePlain target:self action:@selector(buttTow)];
    
    
    
}
    /* 收藏按钮实现 **/
- (void)buttTow{
    Listen *m = [[Listen alloc]init];
    m.listenName = _name;
    // BOOl判断
    if (_isPlay) {
        // 添加
        
        [[DataBaseManager shareInstance]insert:m];
        
    }else{
        // 删除
        [[DataBaseManager shareInstance]deleteMovieWithName:m];
    }
    
    _isPlay = !_isPlay;
    
}

#pragma mark - 头部区域
- (void)headers{
    self.header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 300)];
//        _header.backgroundColor = [UIColor blackColor];
    _tableView.tableHeaderView = _header;
}

#pragma mark - 数据获取
- (void)detaHandler{
    NSString *strr =@"http://api.kaolafm.com/api/v4/audios/list?id=";
    NSString *strTow = @"&sorttype=1&pagesize=20&pagenum=1&installid=0002JX3E&uid=2457975&udid=bd80d675b1bb04bd76de2a42a3aca09d&sessionid=bd80d675b1bb04bd76de2a42a3aca09d1453381639462&imsi=460077347218243&operator=0&lon=0.0&lat=0.0&network=1&timestamp=1453383598&playid=ae58ffc64d28c542f53f0d82080b5e1c&sign=ad0e64ef4e4695f26aab782327753a6b&resolution=720*1280&devicetype=0&channel=B-xiaomi&version=4.5.1&appid=0&token=token517271fe29bfdd7ee055b2e4e34b2eee&";
    NSString *string =[NSString stringWithFormat:@"%@%@%@", strr, _str, strTow];
    NSLog(@"%@", string);
    [AFNetBlock GetDataWithURL:string block:^(id block) {
        NSArray *array = block[@"result"][@"dataList"];
        self.dateArr = [DataModel modelHandlerWithArray:array];
//        // 刷新页面
        [_tableView reloadData];

    }];
}

#pragma mark - tableView的协议设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dateArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    DataModel *m =_dateArr[indexPath.row];
    [cell.imag sd_setImageWithURL:[NSURL URLWithString:m.albumPic]];
    cell.timeLable.text = m.updateTime;
    cell.nimber.text = [m.orderNum stringValue];
    cell.name.text = m.audioName;
    cell.lickLable.text = [m.likedNum stringValue];
    cell.commentLable.text = [m.commentNum stringValue];
    cell.imageOne.image = [UIImage imageNamed:@"ting"];
    cell.imageTow.image = [UIImage imageNamed:@"x"];
    cell.imageThree.image = [UIImage imageNamed:@"pl"];
    cell.termLable.text = @"期:";
    cell.backgroundColor = [UIColor grayColor];
    
    if ([m.listenNum floatValue] > 10000) {
        NSString *str =[NSString stringWithFormat:@"%.1f万",[m.listenNum floatValue ]/ 10000];
        cell.listenLable.text = str;
    }
    
    return cell;
}
// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
// 播放按钮
- (void)butt{
    NSLog(@"butt");

}

// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PlayViewController *play = [[PlayViewController alloc]init];
    play.str= [_dateArr[indexPath.row] mp3PlayUrl];
    play.model =_dateArr[indexPath.row];
    play.strr = _name;
    [self.navigationController pushViewController:play animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
